package socializing

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"project/controller"
	"project/mySql"
)

/**
 * @File : relation.go
 * @Description : 关注列表、粉丝列表以及好友列表
 * @Author : 吴概
 * @Date : 2023/2/11
 */

type RelationResponse struct {
	controller.Response
	UserList []controller.User `json:"user_list"`
}

type FriendResponse struct {
	controller.Response
	FriendList []FriendUser `json:"user_list"`
}
type FriendUser struct {
	Id              int64  `json:"id"`
	Name            string `json:"name" `
	Avatar          string `json:"avatar"`
	BackgroundImage string `json:"background_image"`
	Message         string `json:"message"`
	MsgType         int64  `json:"msgType"`
}

// FollowAction 关注与取消关注操作
func FollowAction(c *gin.Context) {
	actionType := c.Query("action_type")                   //获取行动类型
	currentId := mySql.GetUserByToken(c.Query("token")).Id //当前用户的Id
	toUserId := controller.ParseInt(c.Query("to_user_id")) //操作对象的Id
	if controller.ContainsId(mySql.GetRelation(currentId).FollowList, toUserId) {
		c.JSON(http.StatusOK, controller.Response{StatusCode: 1, StatusMsg: "不能重复关注"})
		return
	}
	if toUserId == 0 {
		c.JSON(http.StatusOK, controller.Response{StatusCode: 1, StatusMsg: "用户已注销"})
		return
	}
	if toUserId == currentId {
		c.JSON(http.StatusOK, controller.Response{StatusCode: 1, StatusMsg: "不能关注自己"})
		return
	}
	currentRelation := mySql.GetRelation(currentId) //获取当前用户的relation
	toUserRelation := mySql.GetRelation(toUserId)   //获取操作对象的relation
	current := mySql.GetUser(currentId)             //当前用户
	toUser := mySql.GetUser(toUserId)               //操作对象

	if actionType == "1" { // 进行关注操作
		currentRelation.FollowList = controller.AddId(currentRelation.FollowList, toUserRelation.Id) //分别在双方的关注列表与粉丝列表中添加对方
		toUserRelation.FollowerList = controller.AddId(toUserRelation.FollowerList, currentRelation.Id)
		if controller.ContainsId(toUserRelation.FollowList, currentRelation.Id) { //判断是否互相关注，若是则添加进好友列表
			currentRelation.FriendList = controller.AddId(currentRelation.FriendList, toUserRelation.Id) //在双方的好友列表中添加对方
			toUserRelation.FriendList = controller.AddId(toUserRelation.FriendList, currentRelation.Id)
		}
		toUser.FollowerCount++ //双方的关注数和粉丝数加一
		current.FollowCount++
	} else { //进行取消关注操作
		currentRelation.FollowList = controller.DeleteId(currentRelation.FollowList, toUserRelation.Id) //分别在双方的关注与粉丝列表中删除对方
		toUserRelation.FollowerList = controller.DeleteId(currentRelation.FollowerList, currentRelation.Id)
		if controller.ContainsId(currentRelation.FriendList, toUserRelation.Id) { //判断是否是好友，若是则删除好友关系
			controller.DeleteId(currentRelation.FriendList, toUserRelation.Id) //删除双方的好友关系
			controller.DeleteId(toUserRelation.FriendList, currentRelation.Id)
		}
		toUser.FollowerCount-- //双方的关注数和粉丝数减一
		current.FollowCount--
	}
	mySql.SaveRelation(currentRelation)
	mySql.SaveRelation(toUserRelation)
	mySql.SaveUser(current)
	mySql.SaveUser(toUser)
	c.JSON(http.StatusOK, controller.Response{StatusCode: 0})
}

// FollowList 关注列表
func FollowList(c *gin.Context) {
	current := mySql.GetRelation(controller.ParseInt(c.Query("user_id")))
	followList := mySql.GetUserListByIdList(controller.GetIdList(current.FollowList))
	for i := range followList { //IsFollow默认为false，需要手动开成true
		followList[i].IsFollow = true
	}
	c.JSON(http.StatusOK, RelationResponse{
		Response: controller.Response{StatusCode: 0},
		UserList: followList,
	})
}

// FollowerList 粉丝列表
func FollowerList(c *gin.Context) {
	current := mySql.GetRelation(controller.ParseInt(c.Query("user_id")))
	idList := controller.GetIdList(current.FollowerList)
	followerList := mySql.GetUserListByIdList(idList)
	//将粉丝中的好友的isFollow参数改成true
	var friendSet = make(map[int]struct{}) //用HashSet保存好友列表以便快速验证好友关系（利用空结构体来优化内存）
	friendList := controller.GetIdList(current.FriendList)
	for _, v := range friendList { //将好友列表存入Set（好友数量 <= 粉丝数）
		friendSet[int(v)] = struct{}{}
	}
	for k, v := range idList { //验证好友关系
		_, ok := friendSet[int(v)]
		if ok {
			followerList[k].IsFollow = true
		}
	}
	c.JSON(http.StatusOK, RelationResponse{
		Response: controller.Response{StatusCode: 0},
		UserList: followerList,
	})
}

// FriendList 好友列表
func FriendList(c *gin.Context) {
	currentId := controller.ParseInt(c.Query("user_id"))
	relation := mySql.GetRelation(currentId)
	friendIdList := mySql.GetUserListByIdList(controller.GetIdList(relation.FriendList))
	friendList := make([]FriendUser, len(friendIdList))
	for k, v := range friendIdList {
		friendList[k].Id = v.Id
		friendList[k].Avatar = v.Avatar
		friendList[k].Name = v.Name
		friendList[k].BackgroundImage = v.BackgroundImage
		messageList := mySql.GetMessageList(mySql.GetChat(currentId, v.Id))
		l := len(messageList) - 1
		if l != -1 {
			friendList[k].Message = messageList[l].Content
			if messageList[l].FromUserId == currentId {
				friendList[k].MsgType = 1
			} else {
				friendList[k].MsgType = 0
			}
		}
	}
	c.JSON(http.StatusOK, FriendResponse{
		Response:   controller.Response{StatusCode: 0},
		FriendList: friendList,
	})
}
